import Vue from 'vue'
import App from './App'
import Request from './utils/request.js'
import {
	upload,uploads
} from 'utils/upload.js'
Vue.config.productionTip = false
Vue.prototype.$http = Request.http
Vue.prototype.$upload = upload
Vue.prototype.$uploads = uploads


// 商家端
import uView from "uview-ui";
Vue.use(uView);
uni.$u.config.unit = 'rpx'

Vue.mixin({
  filters:{
    statusFilter(val) {
			let arr = ['待付款', '待发货', '待收货', '待评价', '已取消', '售后退款中', '已完成', '售后完成']
			return arr[val]
		}
  }
})

Vue.prototype.$back = function () {
  uni.navigateBack({
    delta: 1
  })
}

Vue.prototype.$go= function (url) {
  uni.navigateTo({
    url: url
  })
}
Vue.prototype.$redirect= function (url) {
  uni.redirectTo({
    url: url
  })
}

Vue.filter('dateFormat', function (value) {
  if (!value) return ''
  let date = new Date(value)
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  return year + '-' + month + '-' + day
})


// #ifndef VUE3
App.mpType = 'app'
const app = new Vue({
    ...App
})

//替换富文本中的图片样式
//替换富文本中的图片样式
Vue.prototype.$replace_Rich_text = function(html) {
 let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  match = match.replace(/style\s*?=\s*?([‘"])[\s\S]*?\1/, '').replace(/\style='[^']+'/gi, '');
  match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  return match;
 });
 newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
   'max-width:100%;');
  return match;
 });
 newContent = newContent.replace(/<br[^>]*\/>/gi, '');
 newContent = newContent.replace(/\<img/gi,
  '<img style="max-width:100%;height:auto;display:block;margin:0 auto;"');
 return newContent;
}

Vue.prototype.$navigateTo = function(url) {
  uni.navigateTo({
    url: url
  })
}

try {
  function isPromise(obj) {
    return (
      !!obj &&
      (typeof obj === "object" || typeof obj === "function") &&
      typeof obj.then === "function"
    );
  }

  // 统一 vue2 API Promise 化返回格式与 vue3 保持一致
  uni.addInterceptor({
    returnValue(res) {
      if (!isPromise(res)) {
        return res;
      }
      return new Promise((resolve, reject) => {
        res.then((res) => {
          if (res[0]) {
            reject(res[0]);
          } else {
            resolve(res[1]);
          }
        });
      });
    },
  });
} catch (error) { }
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif